package cn.cloud9.api;


public class API02StringBuilder {

    public static void main(String[] args) {
        commonApi();
    }

    private static void constructor() {

        // 空参可变字符串对象
        StringBuilder stringBuilder = new StringBuilder();

        // 字符串可变构造对象
        StringBuilder stringBuilder2 = new StringBuilder("的撒参加2");
    }

    private static void commonApi() {
        /**
         * 其他方法也有
         *
         */
        StringBuilder sb = new StringBuilder();
        sb.append(1);
        sb.append('a');
        sb.append(11F);
        sb.append(3.14D);
        sb.append(1343L);
        sb.append(new Object());
        sb.append(true);
        sb.reverse();
        System.out.println(sb.toString());

        /**
         *
         * 删除指定下标的字符
         */
        sb.deleteCharAt(10);
        /**
         * 删除下标范围的字符串
         */
        sb.delete(3, 4);

        /**
         *
         * 在下标位置出插入参数字符或者其他数据
         */
        sb.insert(10, 3);

        /**
         * 取下标
         */
        int adsad = sb.indexOf("adsad");


        /* String & StringBuilder 相互转换 */

        // builder -> string
        String a = new StringBuilder().toString();
        // string -> builder
        StringBuilder b = new StringBuilder(a);
    }

    /**
     * https://www.bilibili.com/video/BV18J411W7cE?p=131
     */
    private static void concatStringWithBuilder() {
        int[] array = {1, 2, 3};
        int len = array.length;
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < len; i++) {
            if (i == len - 1) sb.append(array[i]);
            else sb.append(array[i] + ',');
        }
        sb.append(']');
        String result = sb.toString();
    }
}
